package chatServer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import messenger.Message;
import messenger.MessageLog;

public class ConversDB implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8645016116079205372L;
	private ArrayList<UserDB> users = new ArrayList<UserDB>();
	private Hashtable<UserDB, MessageLog> messagelog = new Hashtable<UserDB, MessageLog>();
	private Hashtable<UserDB, ArrayList<Message>> messages = new Hashtable<UserDB, ArrayList<Message>>();

	/**
	 * @param contacts
	 * @param messages
	 */
	public ConversDB() {
		try {
			ObjectInputStream inStream = new ObjectInputStream(
					new FileInputStream("server.data"));
			readObject(inStream);
			inStream.close();
		} catch (IOException e) {

		}
	}

	/**
	 * @param user
	 * @return
	 */
	public int GetMessageCount(UserDB user) {
		if (messages.containsKey(user))
			return messages.get(user).size();

		return 0;
	}

	/**
	 * @return the messagelog
	 */
	public Hashtable<UserDB, MessageLog> getMessagelog() {
		return messagelog;
	}

	/**
	 * @return the messages
	 */
	public Hashtable<UserDB, ArrayList<Message>> getMessages() {
		return messages;
	}

	/**
	 * @param username
	 * @return
	 */
	public UserDB GetUser(String username) {

		for (UserDB u : users)
			if (u.getUserName().equalsIgnoreCase(username))
				return u;
		return null;
	}

	/**
	 * @param username
	 * @param password
	 * @return
	 */
	public UserDB GetUser(String username, String password) {

		for (UserDB u : users)
			if (u.getUserName().equalsIgnoreCase(username)
					&& u.getPassword().equals(password))
				return u;
		return null;
	}

	/**
	 * @return the users
	 */
	public ArrayList<UserDB> getUsers() {
		return users;
	}

	/**
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean MatchUser(String username, String password) {
		for (UserDB u : users)
			if (u.getUserName().equalsIgnoreCase(username)
					&& u.getPassword().equals(password))
				return true;
		return false;
	}

	/**
	 * 
	 * @param inputStream
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void readObject(ObjectInputStream inputStream) throws IOException {
		try {
			this.users = (ArrayList<UserDB>) inputStream.readObject();
			this.messagelog = (Hashtable<UserDB, MessageLog>) inputStream
					.readObject();
			this.messages = (Hashtable<UserDB, ArrayList<Message>>) inputStream
					.readObject();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return
	 */
	public synchronized boolean saveState() {
		try {
			ObjectOutputStream outStream = new ObjectOutputStream(
					new FileOutputStream("server.data"));
			writeObject(outStream);
			outStream.close();
			return true;
		} catch (IOException e) {
			System.err.println("Cannot open/create file");
			return false;
		}

	}

	/**
	 * @param messages
	 *            the messages to set
	 */
	public void setMessages(Hashtable<UserDB, ArrayList<Message>> messages) {
		this.messages = messages;
	}

	/**
	 * @param users
	 *            the users to set
	 */
	public void setUsers(ArrayList<UserDB> users) {
		this.users = users;
	}

	/**
	 * 
	 * @param outputStream
	 * @throws IOException
	 */
	public void writeObject(ObjectOutputStream outputStream) throws IOException {

		outputStream.writeObject(users);
		outputStream.writeObject(messagelog);
		outputStream.writeObject(messages);
	}

}
